﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using Microsoft.Practices.Unity;
using SpectrumAnalyser.Services.Common.Bevaviors;

namespace SpectrumAnalyser.Hosts.Common
{
    /// <summary>
    /// All services of Unity Host will be resolved from unity container.
    /// </summary>
    public class UnityServiceHost : ServiceHost
    {
        private readonly IUnityContainer unityContainer;

        public UnityServiceHost(IUnityContainer unityContainer, Type serviceType, Uri seriveUri)
            : base(serviceType, seriveUri)
        {
            this.unityContainer = unityContainer;
        }

        protected override void OnOpening()
        {
            base.OnOpening();

            if (this.Description.Behaviors.Find<UnityServiceBehavior>() == null)
            {
                this.Description.Behaviors.Add(new UnityServiceBehavior(this.unityContainer));
            }
        }
    }
}